package com.nbjtjc.safe.job.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.mchange.lang.IntegerUtils;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.job.mapper.ScheduleJobMapper;
import com.nbjtjc.safe.job.model.ScheduleJobEntity;
import com.nbjtjc.safe.job.utils.ScheduleJob;
import com.nbjtjc.safe.job.utils.ScheduleUtils;
import com.nbjtjc.safe.mapper.emg.EmgPlanPublicMapper;
import com.nbjtjc.safe.model.emg.EmgPlanPublic;
import com.nbjtjc.safe.util.DateUtils;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @auther guqianbin
 * @date 2018/7/26 16:54
 **/
@Service
public class ScheduleBusinessJobService {

    @Autowired
    private ScheduleJobMapper scheduleJobMapper;

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private EmgPlanPublicMapper emgPlanPublicMapper;

    /**
     * 当应急响应调整时，需要更新定时发送简报的时间
     * @return
     */
    public Boolean updateEmgWorkBriefExecuteDate(int planPublic,int timeInterval){
        EmgPlanPublic emgPlanPublic = emgPlanPublicMapper.getRunningEmgPlanPublic(planPublic);
        Date now = new Date();
        Date publicTime = emgPlanPublic.getSignDate() == null
                ? emgPlanPublic.getCreateDate() : emgPlanPublic.getSignDate();
        List<Date> sendTimeRanges= DateUtils.getEmgSendDate(emgPlanPublic.getTimeInterval(),publicTime);
        Date endDate=sendTimeRanges.get(1);

        List<ScheduleJobEntity> jobs=scheduleJobMapper
                .selectJob("sendWorkBriefAllScheduleTask",planPublic+"-");
        Date sendDate=endDate;
        if(CollectionUtil.isNotEmpty(jobs)){
            jobs.forEach(jobEntity->{
                SimpleTrigger simpleTrigger= ScheduleUtils.getSimpleTrigger(scheduler,jobEntity.getJobId());
                if(!NumberUtil.equals(BigDecimal.valueOf(simpleTrigger.getRepeatInterval())
                        ,BigDecimal.valueOf(timeInterval*3600))){
                    ScheduleUtils.updateSimpleScheduleJob(scheduler,jobEntity,sendDate
                            ,0,emgPlanPublic.getTimeInterval()*3600);
                }
            });
        }
        return true;
    }

    public Result updateWorkBriefSendTask(long jobId,Date sendDate) throws SchedulerException {

        scheduler.deleteJob(ScheduleUtils.getJobKey(jobId));
        ScheduleJobEntity scheduleJobEntity=scheduleJobMapper.selectByPrimaryKey(jobId);
        scheduleJobEntity.setJobTime(sendDate);
        scheduleJobMapper.updateByPrimaryKeySelective(scheduleJobEntity);
        SimpleTrigger simpleTrigger=ScheduleUtils.getSimpleTrigger(scheduler,jobId);
        ScheduleUtils.updateSimpleScheduleJob(scheduler,scheduleJobEntity,sendDate, simpleTrigger.getRepeatCount()
        ,simpleTrigger.getRepeatInterval()/1000);//trigger里面保存都是一毫秒为单位，所以这里要除以1000
        return Result.success(scheduleJobEntity);
    }

    @Transactional
    public Result deleteSendWorkBriefTask(ScheduleJobEntity scheduleJobEntity ) throws SchedulerException {
        if(scheduler.checkExists(ScheduleUtils.getJobKey(scheduleJobEntity.getJobId()))){
            scheduler.deleteJob(ScheduleUtils.getJobKey(scheduleJobEntity.getJobId()));
        }
        scheduleJobMapper.deleteByPrimaryKey(scheduleJobEntity.getJobId());
        return Result.res();
    }


}
